package com.chinasofti.zbest.web;


import com.chinasofti.zbest.dao.model.User;
import com.chinasofti.zbest.service.IUserService;
import com.chinasofti.zbest.web.form.LoginForm;
import com.chinasofti.zbest.web.form.ModifyUserPWFrom;
import com.chinasofti.zbest.web.form.RegForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author zbest
 * @since 2019-05-28
 */
@Controller
@Slf4j
public class UserLoginController extends BaseController {

    @Autowired
    private IUserService iUserService;

    @RequestMapping("tologin")
    public String tologin() {
        log.info("tologin,能跳");
        try {


            return "/user/login";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 登录 方法
     *
     * @param session
     * @param loginInfo
     * @param modelMap
     * @return
     */
    @RequestMapping("login")
    public String login(HttpSession session, LoginForm loginInfo, ModelMap modelMap) {
        User user;
        if (session.getAttribute("user") != null && session.getAttribute("user") != "") {
            log.info(session.getAttribute("user") + "session 不为空");
            /* System.out.println(session + "session 不为空");*/
            User user1 = (User) session.getAttribute("user");
            user = iUserService.login(user1.getUsername(), user1.getPassword());

        } else {
            log.info(session.getAttribute("user") + "session 为空");
            user = iUserService.login(loginInfo.getUsername(), loginInfo.getPassword());
            if (user == null) return "user/login";
        }
        modelMap.addAttribute("user", user);
        session.removeAttribute("user");
        session.setAttribute("user", user);
        session.setMaxInactiveInterval(1000 * 60 * 60 * 24 * 7);
        return "redirect:/";
    }

    /**
     * 登出方法
     *
     * @param session
     * @param loginInfo
     * @param modelMap
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session, LoginForm loginInfo, ModelMap modelMap) {

        session.removeAttribute("user");
        return "redirect:/";
    }

    /**
     * 注册页面
     *
     * @return
     */
    @RequestMapping("toreg")
    public String toreg() {
        return "user/reg";
    }

    /**
     * 注册方法
     *
     * @param regInfo
     * @return
     */
    @RequestMapping("reg")
    public String reg(RegForm regInfo) {
        RegForm regForm = new RegForm();
        BeanUtils.copyProperties(regInfo, regForm);
        iUserService.add(regForm);
        return "user/login";
    }

    @RequestMapping("toindex")
    public String toindex() {
        log.info("跳转主页");
        return "redirect:/";
    }


    /**
     * 可以不要
     * 修改密码的界面
     *
     * @param session
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "modifyPassword",method = RequestMethod.GET)
    public String modifypassword(HttpSession session, ModelMap modelMap) {
        log.info("修改密码的界面");

        User user = (User) session.getAttribute("user");
        modelMap.addAttribute("user",user);

        return "/user/remima";
    }

    /**
     * 修改密码的方法
     *
     * @param session
     * @param loginInfo
     * @param modelMap
     * @return
     */
    @RequestMapping("revampPassword")
    public String revamppassword(HttpSession session, ModifyUserPWFrom loginInfo, ModelMap modelMap) {
        log.info("修改密码的方法");

        if (loginInfo.getCheck() == 1) {
            log.info("使用密码的方式，修改密码");
            User user = iUserService.login(loginInfo.getUsername(), loginInfo.getOldpassword());

            if (user == null) {
                modelMap.addAttribute("msg", "原密码错误,请重输");
                log.info("原密码出错" + loginInfo.getOldpassword());
                return "user/remima";
            }
            log.info("原密码正确" + loginInfo.getOldpassword());
            if (!(loginInfo.getNewpassword().equals(loginInfo.getTwicepassword()))) {
                modelMap.addAttribute("msg", "两次输入的密码不相同，请稳定输入");
                return "user/remima";
            }
            iUserService.modifypass(loginInfo.getUsername(), loginInfo.getNewpassword());
            user.setPassword(loginInfo.getNewpassword());
            session.removeAttribute("user");
            session.setAttribute("user", user);
            session.setMaxInactiveInterval(1000 * 60 * 60 * 24 * 7);
            return "user/login";
            /**
             * 如果此方法存在modelmap绝对跳的是页面
             */
        } else if (loginInfo.getCheck() == 2) {
            log.info("使用短信或邮箱进行修改密码");
            return "";
        } else {
            modelMap.addAttribute("msg", "请选择以下两个方法，进行修改密码");
            return "user/remima";
        }
    }


}

